﻿using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using PBO.Data.Rnd;
using PBO.Data.Util;

namespace PBO.Data.SQLite.DAL
{
    internal static class DALRandomTeam
    {
        /// <summary>
        /// 获取随机用的PmId
        /// </summary>
        public static int[] GetRndPmId()
        {
            const string sql = "select * from v_rnd_ids";
            var dt = SQLiteHelper.DbHelperSQLite.ExecuteDataTable(sql);
            var list = from DataRow dr in dt.Rows select dr[0].ToString().ToInt();
            return list.ToArray();
        }

        /// <summary>
        /// 根据PmId获取Pids
        /// </summary>
        public static int[] GetPids(int pmId)
        {
            string sql = "select pmid from sg_pm_list where pmid=" + pmId;
            var dt = SQLiteHelper.DbHelperSQLite.ExecuteDataTable(sql);
            var list = from DataRow dr in dt.Rows select dr[0].ToString().ToInt();
            return list.ToArray();
        }

        public static PokemonCustomInfo GetSmogonPm(int pid, Random random)
        {
            string sql = "select * from sg_pm_list where pmid=" + pid; // + "order by random()";
            var dt = SQLiteHelper.DbHelperSQLite.ExecuteDataTable(sql);
            var dr = dt.Select().GetRandom(random);
            int pmid = pid;
            var data = SQLiteHelper.Pokemon.GetModel(pid);

            var pm = new PokemonCustomInfo();
            pm.Identity = data.Id;

            pm.NickName = data.NameBase;
            pm.Gender = data.Genders.ToArray().GetRandom(random);

            var ability = (Trait)dr["ability"].ToString().ToInt();
            pm.Ability = ((ability == Trait.None) || !data.Traits.Contains(ability)) ? data.Traits.ToArray().GetRandom(random) : ability;

            pm.Item = (Item)dr["item"].ToString().ToInt();
            pm.Nature = (PokemonNature)dr["nature"].ToString().ToInt();
            pm.LV = 100;
            pm.Iv.SetAll(31);

            #region ev & move

            pm.Ev[StatType.Hp] = dr["hp"].ToString().ToInt();
            pm.Ev[StatType.Atk] = dr["atk"].ToString().ToInt();
            pm.Ev[StatType.Def] = dr["def"].ToString().ToInt();
            pm.Ev[StatType.SpAtk] = dr["spa"].ToString().ToInt();
            pm.Ev[StatType.SpDef] = dr["spd"].ToString().ToInt();
            pm.Ev[StatType.Speed] = dr["spe"].ToString().ToInt();

            for (int i = 0; i < 4; i++)
            {
                string moves = dr["move" + (i + 1)].ToString();
                if (!string.IsNullOrEmpty(moves))
                {
                    var moveids = moves.Split('|').Distinct();
                    var moveid_str = moveids.ToArray().GetRandom(random);
                    var move_id = moveid_str.Split('.').First().ToInt();
                    if (moveid_str.Contains('.'))
                    {
                        moveid_str = moveid_str.ToDouble().ToString("F2");
                        int typeid = moveid_str.Substring(moveid_str.IndexOf('.') + 1).ToInt();
                        pm.Iv = DALHiddenPower.GetHiddenIv(typeid);
                    }
                    pm.Moves[i] = (int)move_id;
                }
            }

            #endregion

            return pm;
        }

    }
}
